#include "../macro.h"
#include <iostream>
#include <unistd.h>
#include <sys/shm.h>
#include <string.h>

struct Buf{
    int useful;
    char text[1024];
};

void write()
{
    int shmid = shmget(1000, sizeof(struct Buf), IPC_CREAT | 0600);
    ERR_CHECK(shmid, -1, "shmget")

    struct Buf *buf = (struct Buf*)shmat(shmid, NULL, 0);
    ERR_CHECK(buf, (struct Buf *)-1, "shmat")

    while(1){
        if(buf->useful == 0){
            memset(buf->text, 0, strlen(buf->text));
            ::read(STDIN_FILENO, buf->text, sizeof(buf->text) - 1);
            buf->useful = 1;
            if(strncmp("end", buf->text, 3) == 0){
                break;
            }
        }
    }
    shmdt(buf);
    printf("write close\n");
}

int main(int argc, char const *argv[])
{
    write(); 
    return 0;
}
